import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/widgets.dart';
import 'package:octo_image/octo_image.dart';

import '../public.dart';

/// 网络图片加载
class ImagesNetworkComponent extends StatelessWidget {
  final String src;
  final String placeholder;
  final double width;
  final double height;
  final BoxFit fit;

  ImagesNetworkComponent(this.src,
      {Key key,
      this.placeholder = 'assets/images/empty_yuedu_grey.png',
      this.width,
      this.height,
      this.fit = BoxFit.cover})
      : super(key: key);

  Widget build(BuildContext context) {
    Widget emptyImage = Image.asset(
      placeholder,
      width: width,
      height: height,
    );

    if (src == null) {
      return emptyImage;
    }
    double size = min(
        min(width ?? double.infinity, height ?? double.infinity) * 0.65, 24);
    return OctoImage(
      width: width,
      height: height,
      fit: fit,
      image: CachedNetworkImageProvider(src),
      placeholderBuilder: (context) {
        return Center(
            child: ImagesGif(
          path: AppConfig.ASSETS_PATH + 'loading',
          duration: Duration(milliseconds: 1200),
          width: size,
          height: size,
          highIndex: 12,
          opacity: 0.5,
        ));
      },
      errorBuilder: (context, a, b) {
        return emptyImage;
      },
    );
  }
}
